﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.Threading;

namespace LuDo.PitEditor
{
    /// <summary>
    /// Wrapper class around ToolTip
    /// </summary>
    class QuickInfo
    {
        private ToolTip Tip { get; set; }
        private RichTextBoxLinks.RichTextBoxEx ParentControl { get; set; }
        private BackgroundWorker bw { get; set; }

        public QuickInfo(RichTextBoxLinks.RichTextBoxEx ctl, string text)
        {
            Tip = new ToolTip();
            this.ParentControl = ctl;
            //Tip.ToolTipTitle = "Errore";
            Tip.BackColor = Color.Red;
            Tip.ForeColor = Color.Green;
            //Tip.AutoPopDelay = 4000;
            //Tip.InitialDelay = 1000;
            //Tip.ReshowDelay = 500;
            //Tip.IsBalloon = false; //Rende il tooltip un fumetto
            Tip.UseAnimation = true;
            Tip.UseFading = true;
            //Tip.ShowAlways = false;
            //Tip.Popup += new PopupEventHandler(Tip_Popup);

            //BackgroundWorker initiate
            bw = new BackgroundWorker();
            bw.WorkerReportsProgress = false;
            bw.WorkerSupportsCancellation = false;
            bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);

            //Showing
            Point point = this.ParentControl.GetPositionFromCharIndex(this.ParentControl.SelectionStart);
            Point p = new Point(point.X, point.Y - 20);
            Tip.Show(text, this.ParentControl, p.X, p.Y);
            bw.RunWorkerAsync();
        }

        void Tip_Popup(object sender, PopupEventArgs e)
        {
            //Size s = e.ToolTipSize;
            //int w = s.Width;
            //int h = s.Height;
        }

        void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            Thread.Sleep(2000);
        }

        void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.Tip.Hide(this.ParentControl);
        }
    }
}
